package com.mzy;

import java.util.Stack;

/**
 * @Author: codermzy
 * @Date: 2024/02/02/22:06
 * @Description:
 */
public class StackArray {

    public static class MyStack {
        private int[] arr;
        private int pushi;
        private int pollli;

        private int size;

        private int limit;

        public MyStack(int limit) {
            arr = new int[limit];
            pushi = 0;
            pollli = 0;
            size = 0;
            this.limit = limit;
        }

        public void push(int value) {
            if (size == limit) {
                System.out.println("栈满了！！！");
                return;
            }
            size++;
            arr[pushi] = value;
            pushi = nextIndex(pushi);
        }

        public int pop() {
            if (size == 0) {
                System.out.println("栈空了！！！");
                return -1;
            }
            size--;
            int ans = arr[pollli];
            pollli = nextIndex(pollli);
            return ans;
        }

        public int nextIndex(int index) {
            return size < limit - 1 ? index + 1 : 0;
        }
    }


    public static class MinStack {

        private Stack<Integer> myStack;
        private Stack<Integer> minStack;

        public MinStack(int limit) {
            this.myStack = new Stack<>();
            this.minStack = new Stack<>();
        }

        public void push(int value) {
            if (myStack.isEmpty()) {
                myStack.push(value);
                minStack.push(value);
                return;
            } else if (minStack.peek() > value) {
                minStack.push(value);
            } else {
                minStack.push(minStack.peek());
            }
            myStack.push(value);
        }

        public int pop() {
            minStack.pop();
            return myStack.pop();
        }

        public int getMin() {
            return minStack.peek();
        }
    }

    public static void main(String[] args) {
        MinStack myStack = new MinStack(3);
        myStack.push(7);
        myStack.push(8);
        myStack.push(10);
        myStack.push(6);
        myStack.pop();
        System.out.println(myStack.getMin());
    }

}
